
// MSAATestDoc.cpp : implementation of the CMSAATestDoc class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "MSAATest.h"
#endif
#include <cassert>

#include "MSAATestDoc.h"

#include <propkey.h>
#include "MSAAPage.h"
#include "MSAATestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMSAATestDoc

IMPLEMENT_DYNCREATE(CMSAATestDoc, CDocument)

BEGIN_MESSAGE_MAP(CMSAATestDoc, CDocument)
END_MESSAGE_MAP()


// CMSAATestDoc construction/destruction

CMSAATestDoc::CMSAATestDoc()
	:m_currentIndex(0)
{
	// TODO: add one-time construction code here

}

CMSAATestDoc::~CMSAATestDoc()
{
}

MSAAPage * CMSAATestDoc::GetPage( int index )
{
	return m_pages[index];
}

MSAAPage *CMSAATestDoc::GetCurrentPage()
{
	return m_pages[m_currentIndex];
}

MSAAPage *CMSAATestDoc::GetPageByPoint(int x, int y)
{
	for(int i = 0; i < m_pages.size(); i++)
	{
		CRect rc = m_pages[i]->GetRect();
		if(x >= rc.left && x <= rc.right
			&& y >= rc.top && y <= rc.bottom)
		{
			return m_pages[i];
		}
	}
	return 0;
}

int CMSAATestDoc::GetPageCount()
{
	return m_pages.size();
}

BOOL CMSAATestDoc::OnOpenDocument( LPCTSTR lpszPathName )
{
	m_filePath = lpszPathName;

	CView* pView = 0;
	POSITION pos = GetFirstViewPosition();
	
	assert (pos != NULL);
	pView = GetNextView(pos);

	m_file.Open(lpszPathName, CStdioFile::modeRead);
	CString line;

	while(m_file.ReadString(line))
	{
		m_fileContents.AddTail(line);

		MSAAPage *page = new MSAAPage((const wchar_t *)line);

		page->m_hWndParent = pView->GetSafeHwnd();
		page->m_pageIndex = m_fileContents.GetCount()-1;
		page->m_document = this;
		page->m_view = dynamic_cast<CMSAATestView *>(pView);


		m_pages.push_back(page);
		NotifyWinEvent(EVENT_OBJECT_CREATE, pView->GetSafeHwnd(), OBJID_CLIENT, page->m_pageIndex+1);
		NotifyWinEvent(EVENT_OBJECT_SELECTION, pView->GetSafeHwnd(), OBJID_CLIENT, page->m_pageIndex+1);

	}
	m_currentIndex = 0;
	NotifyWinEvent(EVENT_OBJECT_FOCUS, pView->GetSafeHwnd(), OBJID_CLIENT, 1);
	return TRUE;
}

BOOL CMSAATestDoc::OnNewDocument()
{
	//if (!CDocument::OnNewDocument())
	//	return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return FALSE;
}




// CMSAATestDoc serialization

void CMSAATestDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

#ifdef SHARED_HANDLERS

// Support for thumbnails
void CMSAATestDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// Modify this code to draw the document's data
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// Support for Search Handlers
void CMSAATestDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// Set search contents from document's data. 
	// The content parts should be separated by ";"

	// For example:  strSearchContent = _T("point;rectangle;circle;ole object;");
	SetSearchContent(strSearchContent);
}

void CMSAATestDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = NULL;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != NULL)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CMSAATestDoc diagnostics

#ifdef _DEBUG
void CMSAATestDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMSAATestDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CMSAATestDoc commands
